package small.common.designPattern.create.singleton;

import java.util.concurrent.ConcurrentHashMap;

public class SingletonRepository<T> {

    private ConcurrentHashMap<T, T> shared;

    public SingletonRepository() {
        this.shared = new ConcurrentHashMap<T, T>(1 << 16);
    }

    private T getSingleton(T value) {
        T t = shared.putIfAbsent(value, value);
        return null == t ? value : t;
    }

    public void remove(T obj) {
        shared.remove(obj);
    }

    private int size(){
        return shared.size();
    }


    public static class SingletonData {

        public static Object getSingleton(Object str) {
            return cache.getSingleton(str);
        }

        public static int getSize() {
            return cache.size();
        }

        private static SingletonRepository<Object> cache = new SingletonRepository();
    }

}
